package com.goodving.guli.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.goodving.guli.R;
import com.goodving.guli.bt.C0668a;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.eventbus.EventUserInfoUpdate;
import com.goodving.guli.bt.model.Birthday;
import com.goodving.guli.bt.model.PersonInfo;
import com.goodving.guli.bt.model.WeightInfo;
import com.goodving.guli.bt.model.WeightInfoManager;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.profile.scale.ScaleWeightInfo;
import com.goodving.guli.bt.ui.DimPanelBottomBar;
import com.goodving.guli.bt.ui.PickAdapter;
import com.goodving.guli.bt.utils.WeightUtil;
import com.goodving.guli.bt.utils.aE;
import com.goodving.guli.bt.weight.UserInfo;
import com.goodving.guli.bt.weight.UserInfoManager;
import com.goodving.guli.bt.weight.WeightWrapper;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.utility.gulihardware.Constant;
import com.goodving.guli.view.C3247b;
import com.goodving.guli.view.C3657a;
import com.goodving.guli.view.C4220a;

import de.greenrobot.event.EventBus;
import kankan.wheel.widget.WheelView;

/**
 * Created by john on 2015/12/9.
 */
public class MemberInfoSetHeightActivity extends MemberInfoBaseActivity implements View.OnClickListener {
    private static final long f11412t = 1000;
    private Context mContext;
    private WheelView f11414i;
    private WheelView f11415j;
    private TextView f11416k;
    private PickAdapter f11417l;
    private int f11418m;
    private float f11419n;
    private WeightWrapper f11420o;
    private PickAdapter f11421p;
    private boolean f11422q;
    private boolean f11423r;
    private boolean f11424s;
    private long f11425u;
    private DimPanelBottomBar mDimPanelBottomBar;

    public MemberInfoSetHeightActivity() {
        this.mContext = this;
        this.f11418m = C4220a.aC;
        this.f11422q = false;
        this.f11423r = false;
        this.f11424s = false;
        this.f11425u = 0;
    }

    private void m15820a() {
        if (this.f11422q) {
            this.f11418m = this.f11420o.m14933c();
        } else {
            this.f11418m = this.f11414i.getRealCurrentItem() + 40;
        }
        GuliLog.tracePrintInfo("MemberInfoBaseActivity", "mHeight = " + this.f11418m);
    }

    private int m15825e() {
        return this.f11422q ? this.f11420o.m14930a() : this.f11418m > 0 ? this.f11418m - 40 : 130;
    }

    public void onCancel () { //m15826c
        m15820a();
        GuliLog.tracePrintInfo("MemberInfoBaseActivity", "onCancel");
        super.onCancel();
    }

    class SyncInfoWithServer implements aE {
        final /* synthetic */ C2017b f11469a;

        SyncInfoWithServer(C2017b c2017b) {
            this.f11469a = c2017b;
        }

        public void onSuccess() { // m15604a
            GuliLog.tracePrintInfo("MemberInfoBaseActivity", "syncinfowithserver onSucess");
            UserInfoManager.getInstance().syncWithServer(this.f11469a.f11468b.mContext);
            WeightInfoManager.getInstance().syncFromServer(this.f11469a.f11468b.mContext);
            this.f11469a.f11468b.f11424s = true;
            C0668a.m8126a(this.f11469a.f11468b.mContext, C0668a.fo);
        }

        public void onFailure() { // m15605b
            GuliLog.tracePrintInfo("MemberInfoBaseActivity", "syncinfowithserver onFailure");
            this.f11469a.f11468b.f11424s = false;
            C0668a.m8126a(this.f11469a.f11468b.mContext, C0668a.fp);
        }
    }

    class C2017b extends AsyncTask<Void, Void, Boolean> {
        final /* synthetic */ UserInfo f11467a;
        final /* synthetic */ MemberInfoSetHeightActivity f11468b;

        C2017b(MemberInfoSetHeightActivity memberInfoSetHeightActivity, UserInfo userInfo) {
            this.f11468b = memberInfoSetHeightActivity;
            this.f11467a = userInfo;
        }

        protected Boolean m15857a(Void... voidArr) {
            GuliLog.tracePrintInfo("MemberInfoBaseActivity", "doInBackground");
            GuliLog.tracePrintInfo("MemberInfoBaseActivity", "start sync with server " + WeightUtil.f11309k);
            this.f11468b.f11424s = false;
            WeightUtil.f11309k = true;
            UserInfoManager.getInstance().m15593b(this.f11468b.mContext, new SyncInfoWithServer(this));
            WeightUtil.f11309k = false;
            GuliLog.tracePrintInfo("MemberInfoBaseActivity", "end sync with server, result ok? " + this.f11468b.f11424s);
            if (!this.f11468b.f11424s) {
                return Boolean.valueOf(false);
            }
            GuliLog.tracePrintInfo("MemberInfoBaseActivity", "before uid " + this.f11467a.uid);
            // save the user information to the table
            UserInfoManager.getInstance().addInfo(this.f11467a, true);
            GuliLog.tracePrintInfo("MemberInfoBaseActivity", "after uid " + this.f11467a.uid);
            return Boolean.valueOf(UserInfoManager.getInstance().syncUserInfoToServer(this.f11468b.mContext, this.f11467a));
        }

        protected void m15858a(Boolean bool) {
            Utils.m7991a((Activity) this.f11468b.mContext);

            // TBD hack here,
            //if (bool.booleanValue()) {
            if (!bool.booleanValue()) {
                Keeper.saveCurrentUser(Keeper.CURRENT_USER_NAME, C3657a.f18328f);
                Keeper.saveCurrentUser(Keeper.CURRENT_USER_GENDER, C3657a.f18328f);
                Keeper.saveCurrentUser(Keeper.CURRENT_USER_BIRTH, C3657a.f18328f);
                Keeper.saveCurrentUser(Keeper.CURRENT_USER_HEIGHT, C3657a.f18328f);
                if (this.f11468b.isWeightChooseUser && this.f11468b.f11393g != null) {
                    this.f11468b.f11393g.m25482a(WeightUtil.m15693a(this.f11468b.f11418m, Birthday.fromStr(this.f11467a.birthday).getAge(), this.f11468b.f11419n));
                    Intent intent = new Intent(this.f11468b, WeightReportActivity.class);
                    intent.putExtra(ScaleWeightInfo.WEIGHTADVDATA_KEY, this.f11468b.f11393g.m25503n());
                    if (this.f11467a != null) {
                        GuliLog.tracePrintInfo("MemberInfoBaseActivity", "info is not null and info is  " + this.f11467a.toString());
                    } else {
                        GuliLog.tracePrintInfo("MemberInfoBaseActivity", "info is null");
                        this.f11467a.uid = 0;
                    }
                    intent.putExtra(UserInfo.USERINFO_KEY, this.f11467a.toJsonString());
                    intent.putExtra("NEW_USER", true);
                    this.f11468b.startActivityForResult(intent, 6);
                    return;
                } else if (this.f11467a != null) {
                    WeightInfo weightInfo = new WeightInfo();
                    weightInfo.uid = this.f11467a.uid;
                    weightInfo.timestamp = System.currentTimeMillis();
                    weightInfo.weight = this.f11468b.f11419n;
                    WeightInfoManager.getInstance().m15749a(weightInfo);
                    EventBus.getDefault().post(new EventUserInfoUpdate(this.f11467a));
                    this.f11468b.setResult(-1);
                    this.f11468b.finish();
                    return;
                } else {
                    return;
                }
            }
            GuliLog.tracePrintInfo("MemberInfoBaseActivity", "onSyncFail");
            UserInfoManager.getInstance().deleteInfoLocal(this.f11467a);
            C3247b.m22930a(this.f11468b.mContext, this.f11468b.getString(R.string.please_connect_internet), 0).show();
        }

        protected /* synthetic */ Boolean doInBackground(Void[] objArr) {
            return m15857a((Void[])objArr);
        }

        protected /* synthetic */ void onPostExecute(Boolean obj) {
            m15858a((Boolean) obj);
        }

        protected void onPreExecute() {
            super.onPreExecute();
            Utils.m7992a((Activity) this.f11468b.mContext, (int) R.string.add_family_member);
        }
    }

    public void onConfirm () { // m15827d
        GuliLog.tracePrintInfo("TIME", "now " + System.currentTimeMillis() + " last time " + this.f11425u);
        if (System.currentTimeMillis() - this.f11425u <= f11412t) {
            GuliLog.tracePrintInfo("TIME", "click too fast, unable to go to next page");
            return;
        }
        GuliLog.tracePrintInfo("TIME", "able to go next");
        this.f11425u = System.currentTimeMillis();
        super.onConfirm();
        m15820a();
        Keeper.saveCurrentUser(Keeper.CURRENT_USER_HEIGHT, this.f11418m + C3657a.f18328f);
        // here i need to check if this is for person info
        PersonInfo personInfo = Keeper.readPersonInfo();
        if (personInfo.nickname.equals(Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_NAME)) &&
                String.valueOf(personInfo.uid).equals(Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_ID))) {
            // we need to get all these information
            personInfo.height = this.f11418m;
            Keeper.keepPersonInfo(personInfo);
        }

        if (this.isVisitorMode || this.isWeightChooseUser) {
            if ((this.isVisitorMode || this.isWeightChooseUser) && this.f11393g != null) {
                this.f11419n = WeightUtil.m15717c(this.f11393g.m25499j(), this.f11393g.m25497h(), 0);
            } else {
                this.f11419n = getIntent().getFloatExtra(Constant.aJ, 70.0f);
            }
            UserInfo userInfo = new UserInfo();
            String currentUserInfoByTag;
            if (this.isVisitorMode) {
                userInfo.name = Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_NAME);
                currentUserInfoByTag = Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_GENDER);
                if (currentUserInfoByTag.isEmpty()) {
                    userInfo.gender = 1;
                } else {
                    userInfo.gender = Integer.parseInt(currentUserInfoByTag);
                }
                userInfo.birthday = Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_BIRTH);
                currentUserInfoByTag = Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_HEIGHT);
                if (currentUserInfoByTag.isEmpty()) {
                    userInfo.height = C4220a.aC;
                } else {
                    userInfo.height = Integer.parseInt(currentUserInfoByTag);
                }
                userInfo.weight = this.f11419n;
                userInfo.uid = 0;
                Keeper.saveCurrentUser(Keeper.CURRENT_USER_NAME, C3657a.f18328f);
                Keeper.saveCurrentUser(Keeper.CURRENT_USER_GENDER, C3657a.f18328f);
                Keeper.saveCurrentUser(Keeper.CURRENT_USER_BIRTH, C3657a.f18328f);
                Keeper.saveCurrentUser(Keeper.CURRENT_USER_HEIGHT, C3657a.f18328f);
                if (this.f11393g != null) {
                    this.f11393g.m25482a(WeightUtil.m15693a(this.f11418m, Birthday.fromStr(userInfo.birthday).getAge(), this.f11419n));
                }
                Intent intent = new Intent(this, WeightReportActivity.class);
                intent.putExtra(ScaleWeightInfo.WEIGHTADVDATA_KEY, this.f11393g == null ? C3657a.f18328f : this.f11393g.m25503n());
                intent.putExtra(UserInfo.USERINFO_KEY, userInfo.toJsonString());
                intent.putExtra("NEW_USER", true);
                startActivityForResult(intent, 6);
                return;
            }
            userInfo.name = Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_NAME);
            currentUserInfoByTag = Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_GENDER);
            if (currentUserInfoByTag.isEmpty()) {
                userInfo.gender = 1;
            } else {
                userInfo.gender = Integer.parseInt(currentUserInfoByTag);
            }
            userInfo.birthday = Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_BIRTH);
            currentUserInfoByTag = Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_HEIGHT);
            if (currentUserInfoByTag.isEmpty()) {
                userInfo.height = this.f11418m;
            } else {
                userInfo.height = Integer.parseInt(currentUserInfoByTag);
            }
            userInfo.weight = this.f11419n;
            GuliLog.tracePrintInfo("MemberInfoBaseActivity", "set weight to : " + this.f11419n + ", info.birthday = " + userInfo.birthday);
            new C2017b(this, userInfo).execute(new Void[0]);
            return;
        }
        Intent intent2 = new Intent(this, MemberInfoSetWeightActivity.class);
        intent2.putExtra(ScaleWeightInfo.WEIGHTADVDATA_KEY, this.f11393g == null ? C3657a.f18328f : this.f11393g.m25503n());
        intent2.putExtra(MemberInfoBaseActivity.setVisitorMode, this.isVisitorMode);
        intent2.putExtra(MemberInfoBaseActivity.weightChooseUser, this.isWeightChooseUser);
        intent2.putExtra("FROM_BABY_WEIGHT", this.f11423r);
        startActivityForResult(intent2, 6);
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        this.isVisitorMode = getIntent().getBooleanExtra(MemberInfoBaseActivity.setVisitorMode, false);
        this.isWeightChooseUser = getIntent().getBooleanExtra(MemberInfoBaseActivity.weightChooseUser, false);
        this.f11423r = getIntent().getBooleanExtra("FROM_BABY_WEIGHT", false);
        if (this.f11423r) {
            this.f11418m = 60;
        }
        if (!(getIntent() == null || getIntent().getExtras() == null)) {
            this.f11393g = ScaleWeightInfo.m25479b(getIntent().getStringExtra(ScaleWeightInfo.WEIGHTADVDATA_KEY));
        }
        setContentView((int) R.layout.activity_person_info_set_height);
        m15808b();
        this.f11422q = Keeper.readPersonInfo().getUnit() != 0;
        this.f11416k = (TextView) findViewById(R.id.bracelet_login_title_info);
        this.f11416k.setText(getString(R.string.input_precise_user_info));
        this.f11414i = (WheelView) findViewById(R.id.person_info_height_picker);
        this.f11421p = new PickAdapter(this, 40, C4220a.an, this.f11414i, getResources().getColor(R.color.title_color), getResources().getColor(R.color.content_color), getResources().getColor(R.color.content_color_darker), false, 50, 48, 45, 45);
        this.f11414i.setVisibleItems(5).setCenterDrawable(R.drawable.wheel_custom_val_dark_0).setCenterStyle(getString(R.string.cm), R.color.content_color, 12, 40.0f, C4220a.bj).setViewAdapter(this.f11421p);
        String currentUserInfoByTag = Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_HEIGHT);
        if (!currentUserInfoByTag.isEmpty()) {
            this.f11418m = Integer.parseInt(currentUserInfoByTag);
        }
        if (this.f11422q) {
            this.f11420o = new WeightWrapper(this.f11418m, this.mContext);
            this.f11415j = (WheelView) findViewById(R.id.info_height_foot_wheel);
            this.f11415j.setVisibility(View.VISIBLE);
            this.f11417l = new PickAdapter(this, 1, 7, this.f11415j, getResources().getColor(R.color.title_color), getResources().getColor(R.color.content_color), getResources().getColor(R.color.content_color_darker), false, 50, 48, 45, 45);
            this.f11415j.setVisibleItems(5).setCenterDrawable(R.drawable.wheel_custom_val_dark_0).setCenterStyle(getString(R.string.cm), R.color.content_color, 12, 38.0f, C4220a.bj).setViewAdapter(this.f11417l);
            this.f11415j.setCurrentItem(this.f11420o.m14932b());
            this.f11420o.m14931a(this.f11415j, this.f11417l, this.f11414i, this.f11421p);
        }
        this.f11414i.setCurrentItem(m15825e());
        this.mDimPanelBottomBar = (DimPanelBottomBar) findViewById(R.id.bottom_bar);
        this.mDimPanelBottomBar.m14980a(getResources().getColor(R.color.content_color));
        this.mDimPanelBottomBar.m14983b(getResources().getColor(R.color.title_color));
    }

    protected void onPause() {
        super.onPause();
        C0668a.m8141b(C0668a.aP);
        C0668a.m8138b((Activity) this);
    }

    protected void onResume() {
        super.onResume();
        C0668a.m8133a(C0668a.aP);
        C0668a.m8124a((Activity) this);
    }
}
